package vn.hanelsoft.newsduet.model.helper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import vn.hanelsoft.newsduet.model.object.ObjectNews;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {

	private static SQLiteDatabase db;
	private final Context mContext;
	String fullPathDB;

	public DBHelper(Context context) {
		super(context, DBDefinition.DATABASE_NAME, null,
				DBDefinition.DATABASE_VERSION);
		this.mContext = context;
		File dbFile = mContext.getDatabasePath(DBDefinition.DATABASE_NAME);
		fullPathDB = dbFile.getAbsolutePath();
//		Log.d("Luong", "=================fullPathDB: "+fullPathDB);
	}

	public void createDataBase() throws IOException {
		// If database not exists copy it from the assets
		if (checkDataBase() == false) {
			this.getReadableDatabase();
			try {
				// Copy the database from assests
				copyDataBase();
				Log.d("Luong", "=================Copy db thanh cong");
			} catch (IOException mIOException) {
				Log.d("Luong", "==============Copy db loi");

				throw new Error("ErrorCopyingDataBase");
			}
		}
	}

	private boolean checkDataBase() {
		File dbFile = new File(fullPathDB);
		return dbFile.exists();
	}

	private void copyDataBase() throws IOException {
		InputStream mInput = mContext.getAssets().open(
				DBDefinition.DATABASE_NAME);
		File dbFile = mContext.getDatabasePath(DBDefinition.DATABASE_NAME);
		String outputPath = dbFile.getAbsolutePath();
		OutputStream mOutput = new FileOutputStream(outputPath);
		byte[] mBuffer = new byte[1024];
		int mLength;
		while ((mLength = mInput.read(mBuffer)) > 0) {
			mOutput.write(mBuffer, 0, mLength);
		}
		mOutput.flush();
		mOutput.close();
		mInput.close();
	}

	public boolean openDB() throws SQLException {
		db = SQLiteDatabase.openDatabase(fullPathDB, null,
				SQLiteDatabase.CREATE_IF_NECESSARY);
		return db != null;
	}

	@Override
	public synchronized void close() {
		if (db != null)
			db.close();
		super.close();
	}

	public void closeDB() {
		db.close();
	}
	//________________ News

	public Cursor getNewsById(int id) {
		openDB();
		Cursor cursor = db.query(DBDefinition.TABLE_NEWS, new String[] {
				DBDefinition.COLUMN_NEWS_ID, DBDefinition.COLUMN_NEWS_CATEGORY_ID,
				DBDefinition.COLUMN_NEWS_LINK, DBDefinition.COLUMN_NEWS_IMAGESRC, DBDefinition.COLUMN_NEWS_DESCRIPTION, DBDefinition.COLUMN_NEWS_PUBDATE, DBDefinition.COLUMN_NEWS_FAVOURITE},
				DBDefinition.COLUMN_NEWS_ID + " = '" + id + "'", null, null, null,
				null);
		return cursor;
	}
	
	public Cursor getNewsByCategoryId(int categoryId) {
		openDB();
		Cursor cursor = db.query(DBDefinition.TABLE_NEWS, new String[] {
				DBDefinition.COLUMN_NEWS_ID, DBDefinition.COLUMN_NEWS_CATEGORY_ID,
				DBDefinition.COLUMN_NEWS_LINK, DBDefinition.COLUMN_NEWS_IMAGESRC, DBDefinition.COLUMN_NEWS_DESCRIPTION, DBDefinition.COLUMN_NEWS_PUBDATE, DBDefinition.COLUMN_NEWS_FAVOURITE },
				DBDefinition.COLUMN_NEWS_CATEGORY_ID + " = '" + categoryId + "'", null, null, null,
				null);
		return cursor;
	}
	
	public Cursor getNewsByFavourite() {
		openDB();
		Cursor cursor = db.query(DBDefinition.TABLE_NEWS, new String[] {
				DBDefinition.COLUMN_NEWS_ID, DBDefinition.COLUMN_NEWS_CATEGORY_ID,
				DBDefinition.COLUMN_NEWS_LINK, DBDefinition.COLUMN_NEWS_IMAGESRC, DBDefinition.COLUMN_NEWS_DESCRIPTION, DBDefinition.COLUMN_NEWS_PUBDATE, DBDefinition.COLUMN_NEWS_FAVOURITE },
				DBDefinition.COLUMN_NEWS_FAVOURITE + " = 1", null, null, null,
				null);
		return cursor;
	}
	
	public void insertNews(String categoryId, String newsLink, String newsImageSRC, String newsDescription, String newsPubDate, String favourite) {
		openDB();
		ContentValues values = new ContentValues();
		values.put(DBDefinition.COLUMN_NEWS_CATEGORY_ID, categoryId);
		values.put(DBDefinition.COLUMN_NEWS_LINK, newsLink);
		values.put(DBDefinition.COLUMN_NEWS_IMAGESRC, newsImageSRC);
		values.put(DBDefinition.COLUMN_NEWS_DESCRIPTION, newsDescription);
		values.put(DBDefinition.COLUMN_NEWS_PUBDATE, newsPubDate);
		values.put(DBDefinition.COLUMN_NEWS_FAVOURITE, favourite);
		db.insert(DBDefinition.TABLE_NEWS, null, values);
		closeDB();
	}
	
	public void deleteNewsById(int id) {
		openDB();
		db.delete(DBDefinition.TABLE_NEWS, DBDefinition.COLUMN_NEWS_ID + " = '" + id
				+ "'", null);
		closeDB();
	}
	
	public void deleteNewsByCategoryId(String categoryId) {
		openDB();
		db.delete(DBDefinition.TABLE_NEWS, DBDefinition.COLUMN_NEWS_CATEGORY_ID + " = " + categoryId, null);
		closeDB();
	}
	
	public void deleteNewsByObjectNews(ObjectNews objectNews) {
		openDB();
		String where = DBDefinition.COLUMN_NEWS_LINK+" = '"+objectNews.getNews_link()+"'";
//		Log.d("Luong", "Luong__________________________Where: "+where);
		db.delete(DBDefinition.TABLE_NEWS, where, null);
		closeDB();
	}
	
	public void updateNewsFavourite(int id, String favourite) {
		openDB();
		ContentValues values = new ContentValues();
		if (favourite != null) {
			values.put(DBDefinition.COLUMN_NEWS_FAVOURITE, favourite);
		}
		db.update(DBDefinition.TABLE_NEWS, values, DBDefinition.COLUMN_NEWS_ID
				+ " = '" + favourite + "'", null);
		closeDB();
	}

	@Override
	public void onCreate(SQLiteDatabase arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

}
